package com.example.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import com.example.server.DialogIntent.OutgoingReceiver;

import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

/*
 * Li Xiaolei: Reason for using socket server to pop up a dialog
 * When the server(A) handling response to location request receives an incoming socket, we want to
 * pop up a dialog to ask user whether allow or deny the request explicitly if RejectDefault is false in Setting
 * However, because server(A) is running in a service(A) where service can not pop up a dialog directly in Android, 
 * so we need service(A) start a new Activity which handle the dialog pop up intent. Then another problem comes that
 * the it pops up after server(A) finishes handling current requesting socket and closes it, which means service(A)
 * can not wait for user to input YES/NO in the pop up dialog. I tried Thread.sleep method in service(A) but the whole
 * application stuck without any response. Even when i created another separate thread or service to handle popping
 * up the dialog, the whole application also stuck when i use Thread.sleep in service(A). So since it can pop up a 
 * dialog after socket server finishes handling one incoming socket in one loop, i created another socket server specially
 * for popping up the dialog without any other use. Although it seems a complex way to implement this, but it works finally. 
 */

public class DialogPopUpService extends Service {

	static public int SEVER_PORT = 8080;
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void onCreate(){
		Thread initBkgThread2 = new Thread(new Runnable(){
			public void run() {
				buildServer();
			}
			
		});
		initBkgThread2.start();
	}
	
	public void buildServer(){

		try {

			Boolean end = false;
		    ServerSocket server = new ServerSocket(SEVER_PORT);
		    while(!end){
		    	//Server is waiting for client here, if needed
		        Socket socket = server.accept();
		        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
	            message.Message msg1 = (message.Message) ois.readObject();
		        Log.d("Tcp Example", "From client: "+msg1.getContent());
		        
				
				Intent dialogIntent = new Intent(getBaseContext(), DialogIntent.class);
				dialogIntent.putExtra("MESSAGE", msg1.getContent());
				dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				getApplication().startActivity(dialogIntent);
				
		        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
		        message.Message msg2 = new message.Message();
		        msg2.setServiceType(0);
                msg2.setContent("Good bye and thanks for all the fish :)");
	            oos.writeObject(msg2);
		        //output.println("Good bye and thanks for all the fish :)");
		        socket.close();
		        //if ( STOPPING conditions){ end = true; }
		    }
		    server.close();
		             
		} catch (UnknownHostException e) {
		 
			 // TODO Auto-generated catch block
		     e.printStackTrace();
		    
		 } catch (IOException e) {
		      
			 // TODO Auto-generated catch block
		     
			 e.printStackTrace();
		     
		 } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
